﻿// <copyright file="Exists.cs" company="EDD Solutions Ltd.">
// Copyright (c) EDD Solutions Ltd. 2010.
//
// This file is part of Sharepoint.Manipulator.Net.
//
// Sharepoint.Manipulator.Net is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Sharepoint.Manipulator.Net is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Sharepoint.Manipulator.Net.  If not, see http://www.gnu.org/licenses/.
// </copyright>
// <author>$Author$</author>
// <date>$Date$</date>
// <revision>$Revision$</revision>
namespace Sharepoint.Manipulator.Net
{
    using System;

    /// <summary>
    /// Class to manage a sharepoint check.
    /// </summary>
    public class Exists : AbstractOperation, IOperation
    {
        /// <summary>
        /// The suboperation to carry out.
        /// </summary>
        private object subOperation;

        #region IOperation Members

        /// <summary>
        /// Run the update operation.
        /// </summary>
        /// <returns>an object containing the result</returns>
        public override object Run()
        {
            bool result = ((IExists)this.Entity).ItemExists(this.ItemId);
            if (result)
            {
                this.RunResponse = "Item Exists";
            }

            return result;
        }

        /// <summary>
        /// Returns the usage information for the operation
        /// </summary>
        /// <returns>a string of instructions</returns>
        public override string Usage()
        {
            return ">app.exe <check|ck> <rooturl>:<siteid>:<entityType>:<entityId>:<itemId> <sub-operation>"
                + Environment.NewLine
                + "available sub-operations are:"
                + Environment.NewLine
                + "'exists' - confirmthe specifed location exists";
        }

        /// <summary>
        /// Parse the incoming parameters
        /// </summary>
        /// <param name="args">the parameters</param>
        public override void ParseParameters(string[] args)
        {
            // args[0] is the location
            if (args.Length != 1)
            {
                throw new Exception("Missing arguments");
            }

            this.ParseLocation(args[0]);
            this.ValidateLocation();
        }

        /// <summary>
        /// Confirm that the operation can be carried out of the location
        /// </summary>
        public override void ValidateLocation()
        {
            // check the entity type is supported.
            switch (this.EntityType)
            {
                case "lists":
                    this.Entity = new List(this.FullSiteUrl, this.EntityId);
                    break;
                default:
                    throw new Exception(string.Format("Unsupported entity type [{0}]", new string[] { this.EntityType }));
            }

            // check the item id is supported.
            int i;
            if (!int.TryParse(this.ItemId, out i))
            {
                throw new Exception(string.Format("Unsupported item id [{0}]", new string[] { this.EntityType }));
            }
        }

        #endregion
    }
}
